package edu.hrbeu.crc.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.hrbeu.crc.mapper.CategoryMapper;
import edu.hrbeu.crc.pojo.entity.Category;
import edu.hrbeu.crc.pojo.vo.CategoryVO;
import edu.hrbeu.crc.service.CategoryService;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {


    /**
     * 通过名称寻找分类
     * @param categoryName
     * @return
     */
    @Override
    public Category getByName(String categoryName) {
        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(Category::getName, categoryName);
        return this.getOne(wrapper);
    }

    /**
     * 通过id查询分类
     * @param id
     * @return
     */
    @Override
    public Category getCategoryById(Integer id) {
        return this.getById(id);
    }

    /**
     * 显示分类信息
     * @param type
     * @return
     */
    @Override
    @Cacheable(cacheNames = "category",key = "#type")
    public List<CategoryVO> display(Integer type) {
        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(Category::getType, type);
        List<Category> list = this.list(wrapper);
        return BeanUtil.copyToList(list, CategoryVO.class);
    }
}
